package nature.nano.component.part;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import nature.nano.component.Strings;
import nature.nano.iface.Accepter;
import nature.nano.iface.enumer.EnumStringCallback;
import nature.nano.iface.secondary.SlotBoard;
import nature.nano.util.accepter.Dummy;

public class SimpleSlotBoard implements SlotBoard {
	
	private Map<String, Accepter> slots = new LinkedHashMap<String, Accepter>();
	private Set<String> disableds = new HashSet<String>();
	
	private static Dummy dummy = new Dummy();

	public SimpleSlotBoard() {
		slots.put(Strings.output, dummy);
		slots.put(Strings.error, dummy);
	}
	
	public void initialize(String...slots) {
		for (String slot : slots) {
			this.slots.put(slot, dummy);
		}		
	}
	
	@Override
	public int enumSlot(EnumStringCallback callback) {
		for (String slot : slots.keySet()) {
			callback.onRead(slot);
		}
		return slots.size();
	}
	
	@Override
	public Accepter plug(String slot, Accepter accepter) {
		if (!slots.containsKey(slot)) {
			return null;
		}
		if (accepter == null) {
			accepter = dummy;
		}
		Accepter old = slots.put(slot, accepter);
		if (old == dummy) {
			return null;
		}
		return old;
	}

	@Override
	public int slotCount() {
		return slots.size();
	}
	
	@Override
	public Accepter getAccepter(String slot) {
		if (disableds.contains(slot)) {
			return null;
		}
		Accepter accepter = slots.get(slot);
		if (accepter == dummy) {
			return null;
		}
		return accepter;
	}
	
	public Accepter slot(String slot) {
		if (disableds.contains(slot)) {
			return dummy;
		}
		return slots.get(slot);
	}

	public void disable(String slot) {
		disableds.add(slot);
	}
	
	public void enable(String slot) {
		disableds.remove(slot);
	}

}
